<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* 车系信息管理
*/
class Car_series extends MY_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Car_series_model');
	}
	//查询列表
	public function index()
	{
		
		$this->load->model('Car_brand_model');
		$this->load->model('Car_type_model');

		$params = array( 'length'=>$this->page_size);
		$params['seriesname'] = $this->input->get_post('seriesname');
		$params['brandid'] = $this->input->get_post('brandid');
		$params['typeid'] = $this->input->get_post('typeid');
		$params['start'] =  intval($this->input->get_post('per_page'));

		$result = $this->Car_series_model->select_page($params);
		
		$result['params'] = $params;

		//分页设置
		$base_url =  site_url("c=car_series&m=index&seriesname=$params[seriesname]");
		$result['pagelink'] = $this->create_page_links($base_url,$result['count']);


		$result['brandlist'] =$this->Car_brand_model->select_list();
		$result['typelist'] =$this->Car_type_model->select_list();
		
		$this->show_view('carSeriesList',$result);
		
	}

	//编辑车系
	public function edit()
	{

		$this->form_validation->set_rules('seriesname', '车辆系列', 'required');

		$seriesid = $this->input->get_post('seriesid');

		$serieseditsubmit = $this->input->post('serieseditsubmit');
		$resultinfo = $this->Car_series_model->select_by_id($seriesid);
		$resultinfo['method'] ='edit';
		if($serieseditsubmit  && $this->form_validation->run())
		{
			$data = array();
			$data['seriesname'] = $this->input->get_post('seriesname');
			$data['brandid'] = $this->input->get_post('brandid');
			$data['typeid'] = $this->input->get_post('typeid');
			$data['seriesimage'] = $this->input->get_post('seriesimage');
			$data['seriesinfo'] = $this->input->get_post('seriesinfo');

			$result = $this->Car_series_model->update($seriesid,$data);
			$this->show_message("编辑车辆系列信息成功",site_url("c=car_series&m=index"));
			
		}
		else
		{	
			$this->load->model('Car_brand_model');
			$this->load->model('Car_type_model');
			$resultinfo['brandlist'] =$this->Car_brand_model->select_list();
			$resultinfo['typelist'] =$this->Car_type_model->select_list();
			
			$this->show_view('carSeriesEdit',$resultinfo);
			
		}
	}
	//新增车系
	public function add()
	{
		$this->form_validation->set_rules('seriesname', '车辆系列', 'required');
		
		$serieseditsubmit = $this->input->post('serieseditsubmit');
		$resultinfo  =array('method'=>'add');
		if($serieseditsubmit  && $this->form_validation->run())
		{
			
			$data = array();
			$data['seriesname'] = $this->input->get_post('seriesname');
			$data['brandid'] = $this->input->get_post('brandid');
			$data['typeid'] = $this->input->get_post('typeid');
			$data['seriesimage'] = $this->input->get_post('seriesimage');
			$data['seriesinfo'] = $this->input->get_post('seriesinfo');
 
			$result = $this->Car_series_model->insert($data);

			$this->show_message("新增车辆系列信息成功",site_url("c=car_series&m=index"));
			
		}
		else
		{		
			$this->load->model('Car_brand_model');
			$this->load->model('Car_type_model');
			$resultinfo['brandlist'] =$this->Car_brand_model->select_list();
			$resultinfo['typelist'] =$this->Car_type_model->select_list();
			
			$this->show_view('carSeriesEdit',$resultinfo);
			
		}
	}
	//删除车系
	public function delete()
	{
		$seriesid = $this->input->get_post('seriesid');
		$result = $this->Car_series_model->delete($seriesid);
		

		if($result['success'])
		{
			$this->show_message("删除车辆系列信息成功",site_url("c=car_series&m=index"));
		}
		else
		{
			$this->show_message($result['message'],site_url("c=car_series&m=index"));
		}

	}

 
}